package com.fafa.swing.base;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @author : fafa
 * @version V1.0
 * @Project: SwingTest
 * @Package com.fafa.swing
 * @Description: JFileChooserDemo
 * @date Date : 2019年11月25日 23:04
 */
public class JFileChooserDemo {

    private JLabel label = new JLabel("所选文件路径：");
    private JTextField jTextField = new JTextField(25);
    private JButton button = new JButton("浏览");
    public JFileChooserDemo(){
        JFrame jf = new JFrame("文件选择器");
        JPanel jPanel = new JPanel();
        jPanel.add(label);
        jPanel.add(jTextField);
        jPanel.add(button);
        jf.add(jPanel);
        jf.setVisible(true);
        jf.setBounds(500,500,400,300);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        button.addActionListener(new MyActionListener());
    }

    private class MyActionListener implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            JFileChooser fc = new JFileChooser("/Users/fafa/");
            int val = fc.showOpenDialog(null);
            if(val == fc.APPROVE_OPTION){
                //正常选择文件
                jTextField.setText(fc.getSelectedFile().toString());
            }else{
                jTextField.setText("未选择文件");
            }
        }
    }

    public static void main(String[] args) {
        new JFileChooserDemo();
    }
}
